package com.zsgeek.wisdchain.qutu.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;

import com.zsgeek.wisdchain.qutu.QuTuApplication;
import com.zsgeek.wisdchain.qutu.base.event.PreferenceHelper;

import java.io.File;

/**
 * Created by 王亨彬 on 2019/1/7.
 */
public class FileUtil {
    //清空文件夹(文件夹不删除)
    public static void cleanDirectory(File file) {
        try {
            if (file.exists()) {
                if (file.isFile()) {
//                    Log.i("FileUtil", "删除文件：" + file.getAbsolutePath());
                    file.delete();
                } else if (file.isDirectory()) { // 否则如果它是一个目录
                    File[] files = file.listFiles(); // 声明目录下所有的文件 files[];
                    if (files != null && files.length > 0)
                        for (int i = 0; i < files.length; i++) {
                            cleanDirectory(files[i]);
                        }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取手机的唯一标识
     * 优先获取权限 Manifest.permission.READ_PHONE_STATE
     *
     * @return
     */
    public static String getPhoneSign() {
        StringBuilder deviceId = new StringBuilder();
        // 渠道标志
        deviceId.append("a");
        try {
            //IMEI号（DeviceId）
            TelephonyManager mTelephonyMgr = (TelephonyManager) QuTuApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
            @SuppressLint("MissingPermission") String imei = mTelephonyMgr.getDeviceId();
            if (!TextUtils.isEmpty(imei)) {
                deviceId.append("imei");
                deviceId.append(imei);
                return deviceId.toString();
            }
//            mTelephonyMgr.getSubscriberId(); //获取IMSI号
            //序列号（sn）
            @SuppressLint("MissingPermission") String sn = mTelephonyMgr.getSimSerialNumber();
            if (!TextUtils.isEmpty(sn)) {
                deviceId.append("sn");
                deviceId.append(sn);
                return deviceId.toString();
            }
            //如果上面都没有， 则生成一个id：随机码
            String uuid = getUUID();
            if (!TextUtils.isEmpty(uuid)) {
                deviceId.append("id");
                deviceId.append(uuid);
                return deviceId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            deviceId.append("id").append(getUUID());
        }
        return deviceId.toString();
    }

    private static String deviceId;

    /**
     * 得到全局唯一UUID
     */
    public static String getUUID() {
        if (deviceId == null) {
            SharedPreferences sharedPreferences = PreferenceHelper.getInstance().getSharedPreferences();
            if (sharedPreferences != null) {
                deviceId = sharedPreferences.getString("uuid", "");
            }
            if (TextUtils.isEmpty(deviceId)) {
                deviceId = AppUtil.generateDeviceId();
//                deviceId = UUID.randomUUID().toString();
                PreferenceHelper.getInstance().getEditor().putString("uuid", deviceId).commit();
            }
        }
        return deviceId;
    }
}
